package com.jalarbee.web.jsf.component;

import com.jalarbee.stock.product.business.ProductAvailability;
import java.io.IOException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 *
 * @author devel
 */
public class UIAvailability extends UIComponentBase {

    @Override
    public String getFamily() {
        return "avail";
    }


    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        ProductAvailability availability = (ProductAvailability) getAttributes().get("value");
        StringBuffer imageName = new StringBuffer("/bismillah/images/icons/avail/");
        if(availability.getDays() == 0) { // there is enough in store
            imageName.append("av");
            imageName.append(availability.getContMin());
            imageName.append("_");
            imageName.append(availability.getContMax());
        } else if (availability.getDays() > 5) {
            imageName.append("in");
            imageName.append(availability.getDays());
        } else if (availability.getDays() == -1) {
            imageName.append("in");
        } else if (availability.getDays() == -2) {
            imageName.append("out");
        } else {
            imageName.append("na");
        }
        imageName.append(".gif");

        writer.append("<img src=\"");
        writer.append(imageName);
        writer.append("\"");
        writer.append(" style=\"border:0\" alt=\"avail\" />");
    }
}
